package net.hcpme.activity;

import net.hcpme.fragment.AdviceFragment;
import net.hcpme.fragment.DataFragment;
import net.hcpme.fragment.MoreFragment;
import net.hcpme.fragment.NoteFragment;
import net.hcpme.fragment.TacticsFragment;
import net.hcpme.service.ReceiveMessageService;
import net.hcpme.util.DummyTabContent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

/**
 * 主Activity，底部TabHost选项卡
 * */
public class HomeActivity extends FragmentActivity {

    private TabHost tabHost;
    private TabWidget tabWidget;
    private LinearLayout bottom_layout;
    private int CURRENT_TAB = 0; // 设置常量
    private TacticsFragment tacticsFragment;// 策略
    private AdviceFragment adviceFragment;// 咨询
    private DataFragment dataFragment;// 数据
    private NoteFragment noteFragment;// 纸条
    private MoreFragment moreFragment;// 更多
    private android.support.v4.app.FragmentTransaction ft;
    private RelativeLayout tabTactics, tabAdvice, tabData, tabNote, tabMore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);

        findTabView();
        tabHost.setup();

        /** 监听 */
        TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {

                /** 碎片管理 */
                android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
                tacticsFragment = (TacticsFragment) fm.findFragmentByTag("tactics");
                adviceFragment = (AdviceFragment) fm.findFragmentByTag("advice");
                dataFragment = (DataFragment) fm.findFragmentByTag("data");
                noteFragment = (NoteFragment) fm.findFragmentByTag("note");
                moreFragment = (MoreFragment) fm.findFragmentByTag("more");
                ft = fm.beginTransaction();

                /** 如果存在Detaches掉 */
                if (tacticsFragment != null)
                    ft.detach(tacticsFragment);

                if (adviceFragment != null)
                    ft.detach(adviceFragment);

                if (dataFragment != null)
                    ft.detach(dataFragment);

                if (noteFragment != null)
                    ft.detach(noteFragment);

                if (moreFragment != null)
                    ft.detach(moreFragment);

                /** 如果当前选项卡是home */
                if (tabId.equalsIgnoreCase("tactics")) {
                    isTabTactics();
                    CURRENT_TAB = 1;
                } else if (tabId.equalsIgnoreCase("advice")) {
                    isTabAdvice();
                    CURRENT_TAB = 2;
                } else if (tabId.equalsIgnoreCase("data")) {
                    isTabData();
                    CURRENT_TAB = 3;
                } else if (tabId.equalsIgnoreCase("note")) {
                    isTabNote();
                    CURRENT_TAB = 4;
                } else if (tabId.equalsIgnoreCase("more")) {
                    isTabMore();
                    CURRENT_TAB = 5;
                }
                ft.commit();
            }

        };
        // 设置初始选项卡
        tabHost.setCurrentTab(0);
        tabHost.setOnTabChangedListener(tabChangeListener);
        initTab();
        /** 设置初始化界面 */
        tabHost.setCurrentTab(0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(this, ReceiveMessageService.class);
        this.stopService(intent);
    }

    // 判断当前
    public void isTabTactics() {
        if (tacticsFragment == null) {
            ft.add(R.id.realtabcontent, new TacticsFragment(), "tactics");
        } else {
            ft.attach(tacticsFragment);
        }
    }

    public void isTabAdvice() {
        if (adviceFragment == null) {
            ft.add(R.id.realtabcontent, new AdviceFragment(), "advice");
        } else {
            ft.attach(adviceFragment);
        }
    }

    public void isTabData() {
        if (dataFragment == null) {
            ft.add(R.id.realtabcontent, new DataFragment(), "data");
        } else {
            ft.attach(dataFragment);
        }
    }

    public void isTabNote() {
        if (noteFragment == null) {
            ft.add(R.id.realtabcontent, new NoteFragment(), "note");
        } else {
            ft.attach(noteFragment);
        }
    }

    public void isTabMore() {
        if (moreFragment == null) {
            ft.add(R.id.realtabcontent, new MoreFragment(), "more");
        } else {
            ft.attach(moreFragment);
        }
    }

    /**
     * 找到Tabhost布局
     */
    public void findTabView() {

        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabWidget = (TabWidget) findViewById(android.R.id.tabs);
        LinearLayout layout = (LinearLayout) tabHost.getChildAt(0);
        TabWidget tw = (TabWidget) layout.getChildAt(1);

        tabTactics = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_layout, tw, false);
        TextView tvTab1 = (TextView) tabTactics.getChildAt(1);
        ImageView ivTab1 = (ImageView) tabTactics.getChildAt(0);
        ivTab1.setBackgroundResource(R.drawable.selector_mood_home);
        tvTab1.setText(R.string.tactics);

        tabAdvice = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_layout, tw, false);
        TextView tvTab2 = (TextView) tabAdvice.getChildAt(1);
        ImageView ivTab2 = (ImageView) tabAdvice.getChildAt(0);
        ivTab2.setBackgroundResource(R.drawable.selector_mood_home);
        tvTab2.setText(R.string.advice);

        tabData = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_layout, tw, false);
        TextView tvTab3 = (TextView) tabData.getChildAt(1);
        ImageView ivTab3 = (ImageView) tabData.getChildAt(0);
        ivTab3.setBackgroundResource(R.drawable.selector_mood_home);
        tvTab3.setText(R.string.data);

        tabNote = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_layout, tw, false);
        TextView tvTab4 = (TextView) tabNote.getChildAt(1);
        ImageView ivTab4 = (ImageView) tabNote.getChildAt(0);
        ivTab4.setBackgroundResource(R.drawable.selector_mood_home);
        tvTab4.setText(R.string.note);

        tabMore = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_layout, tw, false);
        TextView tvTab5 = (TextView) tabMore.getChildAt(1);
        ImageView ivTab5 = (ImageView) tabMore.getChildAt(0);
        ivTab5.setBackgroundResource(R.drawable.selector_mood_home);
        tvTab5.setText(R.string.more);
    }

    /**
     * 初始化选项卡
     * 
     * */
    public void initTab() {
        TabHost.TabSpec tSpecTactics = tabHost.newTabSpec("tactics");
        tSpecTactics.setIndicator(tabTactics);
        tSpecTactics.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecTactics);

        TabHost.TabSpec tSpecAdvice = tabHost.newTabSpec("advice");
        tSpecAdvice.setIndicator(tabAdvice);
        tSpecAdvice.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecAdvice);

        TabHost.TabSpec tSpecData = tabHost.newTabSpec("data");
        tSpecData.setIndicator(tabData);
        tSpecData.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecData);

        TabHost.TabSpec tSpecNote = tabHost.newTabSpec("note");
        tSpecNote.setIndicator(tabNote);
        tSpecNote.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecNote);

        TabHost.TabSpec tSpecMore = tabHost.newTabSpec("more");
        tSpecMore.setIndicator(tabMore);
        tSpecMore.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecMore);
    }

}
